//
//  PersonalSetUpViewController.m
//  suRead
//
//  Created by 龚浩植 on 15/11/12.
//  Copyright © 2015年 bao. All rights reserved.
//

#import "PersonalSetUpViewController.h"
#import "UIViewController+HUD.h"
#import "AccountManager.h"
#import "ServiceNews.h"

#import "YLLAuthorityManager.h"
#import "AccountManager.h"
#import "NewsWebViewController.h"

static NSString *kSeguePersonalAboutViewController = @"PersonalAboutViewController";
static NSString *kSeguePersonalCopyrightStatementViewController = @"PersonalCopyrightStatementViewController";

@interface PersonalSetUpViewController ()<UIGestureRecognizerDelegate,UIAlertViewDelegate>
@property (strong, nonatomic) IBOutlet UILabel *ipLable;

@property (weak, nonatomic) IBOutlet UISwitch *switchPushNotification;
@property (weak, nonatomic) IBOutlet UILabel *labelVersion;

@end

@implementation PersonalSetUpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // enable slide-back
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }

    self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
    self.tableView.sectionFooterHeight = 0;
    
    [self.switchPushNotification addTarget:self action:@selector(switchPushNotification:) forControlEvents:UIControlEventValueChanged];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForegroundNotification) name:UIApplicationWillEnterForegroundNotification object:nil];
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *appVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    self.labelVersion.text = appVersion;
    self.ipLable.text = [AccountManager sharedAccountManager].f_ip;
}

- (void)switchPushNotification:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self appWillEnterForegroundNotification];
}

- (void)appWillEnterForegroundNotification
{
    if ([YLLAuthorityManager authorityWithType:YLLAuthorityType_PushNotification])
    {
        self.switchPushNotification.on = YES;
                
    }
    else {
        self.switchPushNotification.on = NO;
    }
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - Table view data source
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section)
    {
        case 0:
        {
            if (indexPath.row == 1) {
                [self showHUDText:@"清除成功"];
            }
        }
            break;
        case 2:
        {
            if (indexPath.row == 0) {
                NewsWebViewController *controller = [NewsWebViewController new];
                controller.hidesBottomBarWhenPushed = YES;
                controller.urlString = @"http://www.ctdsb.net/index.php?m=content&c=index&a=show&catid=27&id=3953";
                controller.hasShare = NO;
                [self showViewController:controller sender:nil];
                break;
            }
            if (indexPath.row == 1) {
               
                [self performSegueWithIdentifier:kSeguePersonalCopyrightStatementViewController sender:nil];
                break;
            }
            if (indexPath.row == 2) {
                [self performSegueWithIdentifier:kSeguePersonalAboutViewController sender:nil];
                break;
            }
            if (indexPath.row == 3) {
                NewsWebViewController *controller = [NewsWebViewController new];
                controller.hidesBottomBarWhenPushed = YES;
                controller.urlString = @"http://report.12377.cn:13225/toreportinputNormal_anis.do";
                controller.hasShare = NO;
                [self showViewController:controller sender:nil];
                break;
            }
        }
        case 3:
        {
            if (indexPath.row == 0) {
                
                __weak typeof(self) weakSelf = self;
                [ServiceNews doFetchMyIp:^(id responseObject, NSString *message) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        weakSelf.ipLable.text = [AccountManager sharedAccountManager].f_ip;
                    });
                } failure:^(NSString *message) {
                    [weakSelf showHUDText:message];
                }];
            }
            if (indexPath.row == 2) {
                if ([AccountManager sharedAccountManager].f_isLogined) {
                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"退出登录" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil];
                    [alert show];
                }
            }
        }
        default:
            break;
    }
 
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [AccountManager sharedAccountManager].f_isLogined = NO;
        [AccountManager sharedAccountManager].f_nickname = @"";
        [AccountManager sharedAccountManager].f_email = @"";
        [AccountManager sharedAccountManager].f_uid = @"";
        [AccountManager sharedAccountManager].f_userName = @"";
        //清楚cookie
        NSArray *array = [NSHTTPCookieStorage sharedHTTPCookieStorage].cookies;
        for (NSHTTPCookie *cookie in array) {
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
        }
        [self.navigationController popViewControllerAnimated:YES];

    }

}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return YES;
}

@end
